//
//  StayusCellPictureView.swift
//  Weibo
//
//  Created by 明丽 on 16/1/19.
//  Copyright © 2016年 明丽. All rights reserved.
//

import UIKit
private let pictureCellId = "pictureCellId"

private let pictureCellMargin: CGFloat = 5
class StayusCellPictureView: UICollectionView {
    //定义外部变量
    var imageURLs: [NSURL]? {
        //计算配视图的大小
        didSet {
            //计算配视图的大小
            let pSize = caclePictureViewSize()
            //更新约束,一开始我们设置的大小是100,100,现在要跟新跟图片数量有关的大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            testLable.text = "\(imageURLs?.count ?? 0)"
            
            
            reloadData()
        }
    }
    private func caclePictureViewSize() ->CGSize {
        let  imageCount = imageURLs?.count ?? 0
        //没有配图视图
        if imageCount == 0 {
            return CGSizeZero
        }
        //获取集合视图 布局对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //单张配图视图
        if imageCount == 1 {
            
            let itemSize = CGSize(width: 180, height: 120)
            layout.itemSize = itemSize
            
            //显示 等比例显示 图片
            //TODO: 先给定固定尺寸  后期做活
            return itemSize
        }
        
        let itemWidth = (KWidth - StatusCellMargin * 2 - pictureCellMargin * 2) / 3
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        if imageCount == 4 {
            //按照 2 * 2 显示
            let w = 2 * itemWidth + pictureCellMargin
            return CGSizeMake(w , w)
        }
        
        //程序走到这里  2, 3, 5,6,7,8,9
        /*
        1,2,3   ==> 1
        4,5,6   ==> 2
        7,8,9   ==> 3
        */
        //1.确定函数
        let row = CGFloat((imageCount - 1) / 3 + 1)
        
        //计算最大宽度
        let maxW = itemWidth * 3 + 2 * pictureCellMargin
        let maxH = itemWidth * row + (row - 1) * pictureCellMargin
        
        return CGSizeMake(maxW, maxH)
        
    }
    
    
    
    
    
    //找自定义入口
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        //设置间距
        layout.minimumInteritemSpacing = pictureCellMargin
        layout.minimumLineSpacing = pictureCellMargin
        super.init(frame: frame, collectionViewLayout: layout)
        self.backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: 设置视图
    private func setupUI() {
        addSubview(testLable)
        testLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //注册cell
        registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        //设置自己为自己的代理,自己实现数据原方法
        self.dataSource = self
        
    }
    //测试lable
    private lazy var testLable: UILabel = UILabel(title: "", fontSize: 40, color: UIColor.redColor())
    
}
//数据源代理方法
extension StayusCellPictureView: UICollectionViewDataSource{
    //实现数据源方法
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! PictureCell
        //设置模型数据
        cell.imageURL = imageURLs![indexPath.item]
        //区奇数和偶数,判断颜色
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.yellowColor()
        return cell
    }
}
//在一个文件中创建另一个文件
class PictureCell: UICollectionViewCell {
    //获取图片
    var imageURL: NSURL? {
        didSet {
            
            iconView.sd_setImageWithURL(imageURL)
        }
        
        
        
    }
    //自定义入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    private func setupUI() {
        contentView.addSubview(iconView)
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        
        
    }
    
    
    
    //懒加在视图
    private lazy var iconView: UIImageView = {
        let v = UIImageView()
        v.contentMode = .ScaleAspectFill
        //手写代码必须手动裁剪
        v.clipsToBounds = true
        return v
        
    }()
    
}

